/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.maze.utils;

/**
 *
 * @author Thomas
 */
public class StringTool {
    public static String byteArrayToString(byte[] buffer){
        StringBuffer s_buffer = new StringBuffer();
        for (int i = 0; i < buffer.length; i++){
            String hex = Integer.toHexString(0xFF & buffer[i]);
            if(hex.length()==1)
                s_buffer.append('0');
            s_buffer.append(hex);
        }
        return s_buffer.toString();
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
}
